/******************************************************************************\
 * Filename   : enc_common.h
 * Copyright  : 
 * Created    : 2018-10-31 by litao
 * Description:
 * 
\******************************************************************************/

#ifndef __ENC_COMMON_H__
#define __ENC_COMMON_H__
#include <stdint.h>
#include <semaphore.h>

#include "hi_common.h"
#include "hi_comm_sys.h"
#include "hi_comm_vb.h"
#include "hi_comm_vi.h"
#include "hi_comm_vo.h"
#include "hi_comm_venc.h"
#include "hi_comm_vpss.h"
#include "hi_comm_vdec.h"
#include "hi_comm_vda.h"
#include "hi_comm_region.h"
#include "hi_comm_adec.h"
#include "hi_comm_aenc.h"
#include "hi_comm_ai.h"
#include "hi_comm_ao.h"
#include "hi_comm_aio.h"
#include "hi_comm_hdmi.h"
#include "hi_defines.h"

#include "mpi_sys.h"
#include "mpi_vb.h"
#include "mpi_vi.h"
#include "mpi_vo.h"
#include "mpi_venc.h"
#include "mpi_vpss.h"
#include "mpi_vdec.h"
#include "mpi_vda.h"
#include "mpi_region.h"
#include "mpi_adec.h"
#include "mpi_aenc.h"
#include "mpi_ai.h"
#include "mpi_ao.h"
#include "mpi_hdmi.h"

//#include "public.h"
#include "enc_shrm.h"
#include "enc_ai.h"
#include "enc_fifo.h"

#define enc_err		printf
#define enc_warn	printf
#define enc_info	printf

#define ENC_MAX_CHANNEL		 4
#define ENC_SYS_ALIGN_WIDTH  16	
#define ENC_PIXEL_FORMAT     PIXEL_FORMAT_YUV_SEMIPLANAR_420


typedef enum _enc_vi_mode_e
{
	ENC_VI_MODE_4_D1,
	ENC_VI_MODE_4_480I,
	ENC_VI_MODE_4_480P,
	ENC_VI_MODE_4_576I,
	ENC_VI_MODE_4_576P,	
	ENC_VI_MODE_4_720P,
	ENC_VI_MODE_4_1080I,
	ENC_VI_MODE_4_1080P,
	
	ENC_VI_MODE_MAX
}enc_vi_mode_e;
	
typedef enum _enc_vi_chn_set_e
{
    VI_CHN_SET_NORMAL = 0, /* mirror, filp close */ 
    VI_CHN_SET_MIRROR,      /* open MIRROR */   
    VI_CHN_SET_FILP        /* open filp */
}enc_vi_chn_set_e;

typedef enum _enc_rc_e
{
    ENC_RC_CBR = 0,
    ENC_RC_VBR,
    ENC_RC_AVBR,
    ENC_RC_FIXQP
}enc_rc_e;

typedef struct _enc_vi_param_s
{
    HI_S32 s32ViDevCnt;        // VI Dev Total Count    
    HI_S32 s32ViDevInterval;   // Vi Dev Interval    
    HI_S32 s32ViChnCnt;        // Vi Chn Total Count    
    HI_S32 s32ViChnInterval;   // VI Chn Interval
}enc_vi_param_s;

#define ENC_AI_FRAME_MAX_NUM	10
#define ENC_AI_FRAME_MAX_LEN	2048

typedef struct _AUDIO_FRAME_SS
{
    AUDIO_BIT_WIDTH_E   enBitwidth;     /*audio frame bitwidth*/
    AUDIO_SOUND_MODE_E  enSoundmode;    /*audio frame momo or stereo mode*/
    HI_U64  u64TimeStamp;                /*audio frame timestamp*/
    HI_U32  u32Seq;                      /*audio frame seq*/
    HI_U32  u32Len;                      /*data lenth per channel in frame*/
    HI_S8   data[0];
} AUDIO_FRAME_SS;

typedef struct _enc_ai_queue
{
	sem_t  *p_lock;
	HI_U16 read_pos;
	HI_U16 write_pos;
	HI_S8  frame_buffer[ENC_AI_FRAME_MAX_NUM][ENC_AI_FRAME_MAX_LEN];
}enc_ai_queue;

typedef struct _enc_ai_share_block
{
	enc_ai_queue queue[ENC_MAX_CHANNEL];
}enc_ai_share_block;

typedef struct _ENC_VI_INFO
{
	U16 valid;
	U16 width;
	U16 height;
	U16 fps;
	U16 interlace;
} stENC_VI_INFO;

typedef struct _enc_hdmi_dev
{
	int i2c_fd;
	char i2c_dev[32];
	uint8_t i2c_ioaddr;
	uint8_t i2c_cecaddr;
	uint8_t i2c_infoaddr;
	uint8_t i2c_dplladdr;
	uint8_t i2c_ksvaddr;
	uint8_t i2c_edidaddr;
	uint8_t i2c_hdmiaddr;
	uint8_t i2c_cpaddr;
	
	stENC_VI_INFO vi_info;
}enc_hdmi_dev;

typedef struct _enc_main_data
{
	U32 is_firstframe;
	U64 first_pts_shift;
	U32 chan_srcnum;
	U32 chan_mapnum;
	U32 enc_status;
	enc_ai_share_block *p_ai_block;
	qlist_body avout_queue;
	enc_hdmi_dev hdmi_dev;
	pthread_t stm_thread_id;	
	pthread_t ain_thread_id;
	pthread_t out_thread_id;
	pthread_t osd_thread_id;
	int g_shrmid = -1;
	void* g_shrmpt = NULL;
	pENC_DEVSET_MASTER g_encset_mst = NULL;
	pENC_DEVSET_SLAVE g_encset_slv = NULL;
}enc_main_data;

extern enc_main_data main_data;

extern int enc_start_thread(void);
extern int enc_start(stENC_DEVSET_SLAVE *p_slave);
extern int enc_stop(void);

extern HI_U32 enc_sys_CalcPicVbBlkSize(HI_U32 u32AlignWidth,COMPRESS_MODE_E enCompFmt);
extern HI_S32 enc_sys_init(VB_CONF_S *pstVbConf);
extern HI_VOID enc_sys_exit(void);
extern HI_S32 enc_sys_ChanMap(HI_S32 origin_chan);

extern int enc_hdmi_input(void);
extern int enc_hdmi_inputFormat(void);
extern int enc_hdmi_init(void);
extern int enc_init(int chan_num);

extern pENC_DEVSET_SLAVE  	g_encset_slv;
extern int 					g_chlidx;

#endif

